#include "Part.h"
#include <string>

std::vector<Part* > Part::fallenParts;

Part::Part(int startX, int startY, PartType partType, QGraphicsScene *scene)
	: GUIBase()
{
	this->setOriginCenter();

	this->setX(startX);
	this->setY(startY);
	this->setZValue(ZVALUE_PART);
	
	this->partType = partType;

	switch( partType) 
	{
		case A:	loadImage("Resources/Sprites/Letters/A.png"); break;
		case B:	loadImage("Resources/Sprites/Letters/B.png"); break;
		case C:	loadImage("Resources/Sprites/Letters/C.png"); break;
		case D:	loadImage("Resources/Sprites/Letters/D.png"); break;
		case E:	loadImage("Resources/Sprites/Letters/E.png"); break;
		case F:	loadImage("Resources/Sprites/Letters/F.png"); break;
		case G:	loadImage("Resources/Sprites/Letters/G.png"); break;
		case H:	loadImage("Resources/Sprites/Letters/H.png"); break;
		case I:	loadImage("Resources/Sprites/Letters/I.png"); break;
		case J:	loadImage("Resources/Sprites/Letters/J.png"); break;
		case K:	loadImage("Resources/Sprites/Letters/K.png"); break;
		case L:	loadImage("Resources/Sprites/Letters/L.png"); break;
		case M:	loadImage("Resources/Sprites/Letters/M.png"); break;
		case N:	loadImage("Resources/Sprites/Letters/N.png"); break;
		case O:	loadImage("Resources/Sprites/Letters/O.png"); break;
		case P:	loadImage("Resources/Sprites/Letters/P.png"); break;
		case Q:	loadImage("Resources/Sprites/Letters/Q.png"); break;
		case R:	loadImage("Resources/Sprites/Letters/R.png"); break;
		case S:	loadImage("Resources/Sprites/Letters/S.png"); break;
		case T:	loadImage("Resources/Sprites/Letters/T.png"); break;
		case U:	loadImage("Resources/Sprites/Letters/U.png"); break;
		case V:	loadImage("Resources/Sprites/Letters/V.png"); break;
		case W:	loadImage("Resources/Sprites/Letters/W.png"); break;
		case X:	loadImage("Resources/Sprites/Letters/X.png"); break;
		case Y:	loadImage("Resources/Sprites/Letters/Y.png"); break;
		case Z:	loadImage("Resources/Sprites/Letters/Z.png"); break;
		case SPACE:	loadImage("Resources/Sprites/Letters/EMPTY.png"); break;
		default: loadImage("Resources/Sprites/Letters/EMPTY.png"); break;
	}
}
Part::~Part()
{
	this->RemoveFromFloor(); 
	//as a safety, whenever a part is deleted, it is also removed from the fallen parts vector 
}

Part::Part(PartType partType)
{ //constructor loads specific image for each letter, creates a generic part
	this->partType = partType;

	switch( partType) 
	{
		case A:	loadImage("Resources/Sprites/Letters/A.png"); break;
		case B:	loadImage("Resources/Sprites/Letters/B.png"); break;
		case C:	loadImage("Resources/Sprites/Letters/C.png"); break;
		case D:	loadImage("Resources/Sprites/Letters/D.png"); break;
		case E:	loadImage("Resources/Sprites/Letters/E.png"); break;
		case F:	loadImage("Resources/Sprites/Letters/F.png"); break;
		case G:	loadImage("Resources/Sprites/Letters/G.png"); break;
		case H:	loadImage("Resources/Sprites/Letters/H.png"); break;
		case I:	loadImage("Resources/Sprites/Letters/I.png"); break;
		case J:	loadImage("Resources/Sprites/Letters/J.png"); break;
		case K:	loadImage("Resources/Sprites/Letters/K.png"); break;
		case L:	loadImage("Resources/Sprites/Letters/L.png"); break;
		case M:	loadImage("Resources/Sprites/Letters/M.png"); break;
		case N:	loadImage("Resources/Sprites/Letters/N.png"); break;
		case O:	loadImage("Resources/Sprites/Letters/O.png"); break;
		case P:	loadImage("Resources/Sprites/Letters/P.png"); break;
		case Q:	loadImage("Resources/Sprites/Letters/Q.png"); break;
		case R:	loadImage("Resources/Sprites/Letters/R.png"); break;
		case S:	loadImage("Resources/Sprites/Letters/S.png"); break;
		case T:	loadImage("Resources/Sprites/Letters/T.png"); break;
		case U:	loadImage("Resources/Sprites/Letters/U.png"); break;
		case V:	loadImage("Resources/Sprites/Letters/V.png"); break;
		case W:	loadImage("Resources/Sprites/Letters/W.png"); break;
		case X:	loadImage("Resources/Sprites/Letters/X.png"); break;
		case Y:	loadImage("Resources/Sprites/Letters/Y.png"); break;
		case Z:	loadImage("Resources/Sprites/Letters/Z.png"); break;
		case SPACE:	loadImage("Resources/Sprites/Letters/EMPTY.png"); break;
		default: loadImage("Resources/Sprites/Letters/EMPTY.png"); break;
	}
}

void Part:: PartFellOnFloor() //when lanes purge or gantry drops parts, this adds them to a vector to be picked up by roomba 
{
	//in case this function is called twice, this check will prevent it from crashing
	for(int i=0; i<fallenParts.size(); i++)
	{
		if(fallenParts[i]==this) 
		{	
			//TraceManager:: SharedManager()->addMessage("Part fell on floor twice", time(NULL), this);
			return;
		}	
	}

	int b;
	Part* temp; //insertion sort puts parts in vector in order from bottom to top
	for(int a=0; a<fallenParts.size(); a++)
	{		//will pick up parts from lane 7 to lane 0
		b=a;
		while(b>0 && fallenParts[b-1]->y() <= fallenParts[b]->y()) 
		{
			temp=fallenParts[b];
			fallenParts[b]=fallenParts[b-1];
			fallenParts[b-1]=temp;
			b--;
		}
	}
	
	fallenParts.push_back(this);
	this->setZValue(ZVALUE_GROUND);
}

void Part::RemoveFromFloor() //when parts are picked up by roomba they are removed from the vector 
{
	for(int i=0; i<fallenParts.size(); i++)
	{
		if(this == fallenParts[i])
			fallenParts.erase(fallenParts.begin()+i);
	}
}

void Part::beganAscending(qreal newScale, qreal scaleSpeed)
{
	this->setScaleSpeed(scaleSpeed);
	this->scaleOverTime(newScale);
}

void Part::beganDescending(qreal newScale, qreal scaleSpeed)
{
	this->setScaleSpeed(scaleSpeed);
	this->scaleOverTime(newScale);
}
